package io.renren.controller;

import java.text.ParseException;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.InterviewEntity;
import io.renren.service.InterviewService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-27 22:26:12
 */
@RestController
@RequestMapping("interview")
public class InterviewController extends AbstractController{
	@Autowired
	private InterviewService interviewService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("interview:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<InterviewEntity> interviewList = interviewService.queryList(query);
		int total = interviewService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(interviewList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("interview:info")
	public R info(@PathVariable("id") Integer id){
		InterviewEntity interview = interviewService.queryObject(id);
		
		return R.ok().put("interview", interview);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("interview:save")
	public R save(@RequestBody InterviewEntity interview){
		interviewService.save(interview);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("interview:update")
	public R update(@RequestBody InterviewEntity interview){
		interviewService.update(interview);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("interview:delete")
	public R delete(@RequestBody Integer[] ids){
		interviewService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 根据面试不同的阶段，不同的结果，更新面试表
	 * 一面：通过，不通过，跳过二面，跳过体检
	 * 二面：通过，不通过，跳过体检
	 * 体检：通过，不通过，免体检，延期。。。
	 */
	@RequestMapping("/updateStatus")
	@RequiresPermissions("interview:update")
	public R updateInterviewStatus(@RequestBody JSONObject jsonObject){
		try {
			interviewService.updateFirstInterviewStatus(jsonObject);
		} catch (ParseException e) {
			e.printStackTrace();
			return R.error("日期格式转换异常!");
		}
		return R.ok();
	}

	/**
	 * 根据参数获取不同面试阶段的数据
	 */
	@RequestMapping("/getInfoBystatus")
	@RequiresPermissions("interview:list")
	public R getInterviewInfoByStatus(@RequestParam Map<String, Object> params){
		/*Set<String> set = params.keySet();
		Iterator<String> it = set.iterator();
		while(it.hasNext()){
			String key = it.next();
			System.out.println(key + ":" + params.get(key));
		}*/
		//Map<String, Object> map = new HashMap<String, Object>();
		String status = params.get("interviewStatus").toString();
		if("firstInterview".equals(status)){
			params.put("first_interview_status",true);
		}else if("secondInterview".equals(status)){
			params.put("second_interview_status",true);
		}else if("physicalExamination".equals(status)){
			params.put("physical_examination_status",true);
		}
		List<String> list = new ArrayList<>();
		list.add("0");
		list.add("1");
		params.put("status",list);
		Query query = new Query(params);
		List<InterviewEntity> res = interviewService.getInterviewByStatus(query);
		int total = interviewService.queryTotalInterview(query);
		PageUtils pageUtil = new PageUtils(res, total, query.getLimit(), query.getPage());


		return R.ok().put("page", pageUtil);
	}
	/**
	 * 获取招聘结算管理列表
	 */
	@RequestMapping("/SettlementList")
	@RequiresPermissions("interview:SettlementList")
	public R SettlementList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<InterviewEntity> interviewList = interviewService.querySettlementList(query);
		int total = interviewService.querySettlementTotal(query);

		PageUtils pageUtil = new PageUtils(interviewList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	/**
	 * 系统确认
	 */
	@RequestMapping("/sysconfirm")
	public R sysConfirm(){
				interviewService.sysConfirm();
		return R.ok();
	}
	/**
	 * 系统确认
	 */
	@RequestMapping("/confirm")
	public R confirm(@RequestBody JSONObject jsonObject){
		interviewService.confirm(jsonObject);
		return R.ok();
	}

	/**
	 * 结算
	 */
	@RequestMapping("/addBatchCode")
	public R addBatchCode(@RequestBody JSONObject jsonObject){
		interviewService.addBatchCode(jsonObject);
		return R.ok();
	}
	/**
	 * 审核
	 */
	@RequestMapping("/audit")
	public R audit(@RequestBody JSONObject jsonObject){
		interviewService.audit(jsonObject,getUser());
		return R.ok();
	}
}
